<?php
class Administrator_Model_News_View extends Administrator_Model_News_Model{
	public function __construct(){
		$this->db = Zend_Registry::get('vicdbsystem');
		$this->lang = new Nguyen_language_system();
	}
	//temp
	public function GetUserInfo($id){
		$sql = 'select * from user where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}
	///////////               ajax              ///////////////////////
	
	
	
	
	
	/////////////////             section                ///////////////
	public function GetSectionInfo($id){
		$sql = 'select * from news_section where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}
	public function GetSectionByCat($catid){
		$sql = 'select section from news_cat where id='.$catid;
		$result = $this->db->fetchRow($sql);
		return $result['section'];
	}
	public function GetAllSection(){
		$sql = 'select * from news_section';
		return $this->db->fetchAll($sql);
	}	
	public function countContentinCat($catid){
		$sql = 'select * from news_content where parent="'.$catid.'"';
		return count($this->db->fetchAll($sql));
	}	
	public function countCatinSection($sectionid){
		$sql = 'select * from news_cat where section="'.$sectionid.'"';
		return count($this->db->fetchAll($sql));
	}
	public function countContentinSection($sectionid){
		$sql = 'select news_content.* from news_cat,news_content where news_cat.id = news_content.parent and news_cat.section="'.$sectionid.'"';
		return count($this->db->fetchAll($sql));
	}
	public function deleteSection($id){	
		if($this->countCatinSection($id)>0) return ;	
		$info = $this->GetSectionInfo($id);
		$this->deleteLangContent($info['name']);
		$where = "id='".$id."'";
		$this->db->delete('news_section',$where);
	}
	/////////////////             catalogue                ///////////////
	public function GetCatalogueInfo($id){
		$sql = 'select * from news_cat where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}

	public function GetAllCatalogue(){
		$sql = 'select * from news_cat';
		return $this->db->fetchAll($sql);
	}
	public function GetAllCatBySection($sectionid){
		$sql = 'select * from news_cat where section='.$sectionid;
		return $this->db->fetchAll($sql);
	}
	public function deleteCatalogue($id){		
		if($this->countContentinCat($id)>0) return ;
		$info = $this->GetCatalogueInfo($id);
		$this->deleteLangContent($info['name']);
		$this->deleteLangContent($info['intro']);
		$this->DeleteImageOnHost('news', $info['image']);
		$where = "id='".$id."'";
		$this->db->delete('news_cat',$where);
	}
	/////////////////             content                ///////////////
	public function GetContentInfo($id){
		$sql = 'select * from news_content where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}
	public function GetAllContent(){
		$sql = 'select * from news_content';
		return $this->db->fetchAll($sql);
	}
	public function deleteContent($id){		
		$info = $this->GetContentInfo($id);
		$this->deleteLangContent($info['name']);
		$this->deleteLangContent($info['intro']);
		$this->deleteLangContent($info['content']);
		$this->DeleteImageOnHost('news', $info['image']);
		$this->deleteContentTagByContent($id);
		$where = "id='".$id."'";
		$this->db->delete('news_content',$where);
	}
	/////////////////             content_tag                ///////////////

	public function GetTagname($id){
		$sql = 'select name from news_tag where id='.$id;
		$result =  $this->db->fetchRow($sql);
		return $result['name'];
	}
	public function GetContentByTag($tag){
		$sql = 'select content from news_content_tag where tag='.$tag;
		return $this->db->fetchAll($sql);
	}
	public function GetTagByContent($content){
		$sql = 'select tag from news_content_tag where content='.$content;
		return $this->db->fetchAll($sql);
	}
	
	
	
	
	
	
	
}